import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrlProduct = CommonConfig.processUrl + '/v1'

import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// 列举产品列表
export function getProduct (keyword, pageNum, pageSize, productPropertyCode, productStatusCode, productTypeCode, searchBy, showNoProcess) {
  const newUrl = `${baseUrlProduct}/products`

  let reqData = {
    params: {
      keyword: keyword || undefined,
      pageNum,
      pageSize,
      productPropertyCode: productPropertyCode === 'all' ? null : productPropertyCode,
      productStatusCode: productStatusCode === 'all' ? null : productStatusCode,
      productTypeCode: productTypeCode === 'all' ? null : productTypeCode,
      searchBy: searchBy || undefined,
      showNoProcess
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 批量删除
export function batchDeletion (ids) {
  const newUrl = `${baseUrlProduct}/products`

  let config = {
    params: {
      ids
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }

  return axiosInstance.delete(newUrl, config).then(resp => {
    return Promise.resolve(resp)
  })
}

// 删除单个产品
export function deleteProduct (productId) {
  const newUrl = `${baseUrlProduct}/products/${productId}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 导出选中的excel
export function downloadProductExcel (data) {
  const newUrl = `${baseUrlProduct}/products/excel:export`

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 新增产品
export function addProduct (query) {
  const url = `${baseUrlProduct}/products`

  return axiosInstance.post(url, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 编辑产品
export function editProduct (query) {
  const url = `${baseUrlProduct}/products/${query.id}`

  let params = {
    ...query
  }

  return axiosInstance.put(url, params).then(resp => {
    return Promise.resolve(resp)
  })
}
